/**
 * Created by dave_suwei on 2016/6/1.
 */

(function($){
    function renderDialog() {
        if (typeof($("#messageDialog")[0]) == 'undefined') {
            var dialogTitle = "消息";
            var htmlStr = "" +
                "<div class='modal fade' id='messageDialog' tabindex='-1' role='dialog' style='z-index: 20000'>                                                               " +
                "    <div class='modal-dialog'>                                                                                                                 " +
                "        <div class='modal-content'>                                                                                                                     " +
                "            <div class='modal-header'>                                                                                                                  " +
                "                <button type='button' class='close' data-dismiss='modal'>&times;</button>                                                               " +
                "                <h4 class='modal-title' id='dinging_lable'>" + dialogTitle + "</h4>                                                                                " +
                "            </div>                                                                                                                                      " +
                "            <div class='modal-body' id='msgBody' style='margin: 10px;'>                                                                                                                    " +
                "            </div>                                                                                                                                      " +
                "           <div class='modal-footer'>" +
                "               <button type='button' class='btn btn-default' data-dismiss='modal'>关闭</button>" +
                "           </div>" +
                "        </div>                                                                                                                                          " +
                "    </div>                                                                                                                                              " +
                "</div>                                                                                                                                                  ";

            var dialog = $(htmlStr);
            $("body").append(htmlStr);
        }
    }
    function addWaiting(){
        //先判断是否存在
        var waiting=$("#sw-straining")[0];
        //console.log(typeof(waiting));
        if(typeof(waiting) == "undefined"){
            var htmlStr="<div class='sw-straining' id='sw-straining'>"+
                "<div class='load-container load'>"+
                "    <div class='loader'>Loading...</div>"+
                "   </div>"+
                "</div>";
            $("body").append(htmlStr);
        }
    }
    function showWaiting(){
        var waiting=$("#sw-straining")[0];
        //console.log(typeof(waiting));
        if(typeof(waiting) == "undefined"){
            addWaiting();
        }
        waiting=$("#sw-straining");
        waiting.css("display","block");
    }
    function hideWaiting(){
        var waiting=$("#sw-straining");
        if(typeof(waiting) == "undefined"){
            return;
        }
        waiting.css("display","none");
    }
    //路标添加
    $.fileUpload=function (url,webContext,form,callBack){
        renderDialog();
        var FileController = url;                    // 接收上传文件的后台地址
        // XMLHttpRequest 对象
        var xhr = null;
        showWaiting();
        if (window.XMLHttpRequest)
        {// code for all new browsers
            xhr=new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {// code for IE5 and IE6
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xhr!=null)
        {
            xhr.onreadystatechange=state_Change;
            xhr.open("post", FileController, true);
            xhr.onload = function (data) {
                //alert("上传完成!"+data);
                hideWaiting();
            };
            xhr.send(form);
        }
        else
        {
            alert("Your browser does not support XMLHTTP.");
        }

        function state_Change()
        {
            if (xhr.readyState==4)
            {// 4 = "loaded"
                if (xhr.status==200)
                {// 200 = OK
                    // ...our code here.
                    var response=xhr.responseText;
                    callBack(response);
                }else if (xhr.status === 403) {//弹出无权限窗口
                    var defaultUrl = webContext + 'auth/default';
                    var loginUrl = webContext + 'auth/login';
                    var body = "<div style='text-align: center;'>" +
                        "没有权限，<a href='" + defaultUrl + "?from=index'>返回默认页</a>，<a href='" + loginUrl + "'>重新登录</a>" +
                        "</div>";
                    $("#msgBody").html(body);
                    $("#messageDialog").modal("show");
                } else if (xhr.status === 500) {
                    var indexUrl = webContext + 'index.jsp';
                    var body = "<div style='text-align: center;'>" +
                        "服务器出了点小问题，<a href='" + indexUrl + "'>返回主页</a>" +
                        "</div>";
                    $("#msgBody").html(body);
                    $("#messageDialog").modal("show");
                }
                else
                {
                    alert("Problem retrieving XML data");
                }
            }
        }
    }
})(jQuery);
